import { useSearchParams } from "react-router-dom";
import { getQuestionList } from "../services/question";
import { useRequest } from "ahooks";
import { LIST_SEARCH_PARAM_KEY,LIST_PAGE_PARAM_KEY,LIST_PAGE_SIZE_PARAM_KEY } from "../constant/index";

type OptionType = {
  isStar: boolean;
  isDeleted: boolean;
}
function useSearchQuestionData(opt:Partial<OptionType>={}) {
  const { isStar, isDeleted } = opt
  const [searchParams] = useSearchParams();

  async function fn() {
    const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || "";
    const page = parseInt(searchParams.get(LIST_PAGE_PARAM_KEY)||'') || 1;
    const pageSize = parseInt(searchParams.get(LIST_PAGE_SIZE_PARAM_KEY)||'') || 10;

    const data = await getQuestionList({keyword, isStar, isDeleted,page,pageSize})
    return data
  }
  /**
   * refresh:重新加载数据
   */
  const {loading,data,error,refresh} = useRequest(fn,
    {
      refreshDeps: [searchParams] // 刷新依赖项
    }
  )

  return {loading,data,error,refresh}
}
export default useSearchQuestionData;